package com.css.zfzx.sjcj.modules.drill.repository.impl;

import com.css.bpm.platform.api.local.PlatformAPI;
import com.css.bpm.platform.org.dept.repository.entity.DeptEntity;
import com.css.bpm.platform.org.division.repository.DivisionRepository;
import com.css.bpm.platform.org.division.repository.entity.DivisionEntity;
import com.css.bpm.platform.org.role.repository.entity.RoleEntity;
import com.css.bpm.platform.utils.PlatformObjectUtils;
import com.css.bpm.platform.utils.PlatformSessionUtils;
import com.css.zfzx.sjcj.common.utils.ServerUtil;
import com.css.zfzx.sjcj.modules.drill.Entity.QhY02Entity;
import com.css.zfzx.sjcj.modules.drill.repository.MyQhRepository;
import com.css.zfzx.sjcj.modules.drill.repository.QhY02NativeRepository;
import com.css.zfzx.sjcj.modules.drill.service.QhY18Service;
import com.css.zfzx.sjcj.modules.drill.viewobjects.QhY02QueryParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Repository;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import java.math.BigInteger;
import java.util.List;

/**
 * @Description
 * @Author Li Helei
 * @Date 2021/1/15 17:10
 * @Version V1.0
 */
@Repository
@PropertySource("classpath:platform-config.yml")
public class QhY02NativeRepositoryImpl implements QhY02NativeRepository {
    @PersistenceContext
    private EntityManager em;
    @Value("${role.superCode}")
    private String superCode;
    @Value("${role.provinceCode}")
    private String provinceCode;
    @Value("${role.cityCode}")
    private String cityCode;
    @Value("${role.areaCode}")
    private String areaCode;
    @Autowired
    private DivisionRepository divisionRepository;
    @Autowired
    MyQhRepository myQhRepository;
    @Autowired
    private QhY18Service qhY18Service;

    private static final Logger log = LoggerFactory.getLogger(QhY02NativeRepositoryImpl.class);

    @Override
    public Page<QhY02Entity> queryQhY02s(QhY02QueryParams queryParams, int curPage, int pageSize, String sort, String order) {
        StringBuilder sql = new StringBuilder("select * from qh_y02 ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        // 省
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        // 市
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city = :city");
        }
        // 区
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area = :area");
        }
        // 乡镇
        if (!PlatformObjectUtils.isEmpty(queryParams.getVillage())) {
            whereSql.append(" and village like :village");
        }
        // 钻孔综合编号
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillSynthesizeCode())) {
            whereSql.append(" and drill_synthesize_code like :drillSynthesizeCode");
        }
        //数据来源类型(SK,PK)
        if (!PlatformObjectUtils.isEmpty(queryParams.getDataSourceType())) {
            whereSql.append(" and drill_synthesize_code like :dataSourceType");
        }
        // 数据状态
        if (!PlatformObjectUtils.isEmpty(queryParams.getDataState())) {
            whereSql.append(" and data_state = :dataState");
        }
        // 孔位经度
        if (!PlatformObjectUtils.isEmpty(queryParams.getLongitude())) {
            whereSql.append(" and longitude like :longitude");
        }
        // 孔位纬度
        if (!PlatformObjectUtils.isEmpty(queryParams.getLatitude())) {
            whereSql.append(" and latitude like :latitude");
        }
        // 钻孔深度开始
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillDepthBegin())) {
            whereSql.append(" and drill_depth >= :drillDepthBegin");
        }
        // 钻孔深度结束
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillDepthEnd())) {
            whereSql.append(" and drill_depth <= :drillDepthEnd");
        }
        // 钻孔勘察单位
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillConstructionInstitution())) {
            whereSql.append(" and drill_construction_institution like :drillConstructionInstitution");
        }
        // 勘察施工日期开始 前端只传年月日
        if (!PlatformObjectUtils.isEmpty(queryParams.getConstructionStartDateBegin())) {
            //queryParams.setConstructionStartDateBegin(queryParams.getConstructionStartDateBegin() + "-01-01");
            whereSql.append(" and construction_start_date >= :constructionStartDateBegin");
        }
        // 勘察施工日期结束
        if (!PlatformObjectUtils.isEmpty(queryParams.getConstructionStartDateEnd())) {
            //queryParams.setConstructionStartDateEnd(queryParams.getConstructionStartDateEnd() + "-12-31");
            whereSql.append(" and construction_start_date <= :constructionStartDateEnd");
        }
        // 场地类型
        if (!PlatformObjectUtils.isEmpty(queryParams.getSiteType())) {
            whereSql.append(" and id in :ids");
        }
        // 项目完成单位
        if (!PlatformObjectUtils.isEmpty(queryParams.getProjectCompleteInstitution())) {
            whereSql.append(" and project_complete_institution like :projectCompleteInstitution");
        }
        // 项目完成时间
        if (!PlatformObjectUtils.isEmpty(queryParams.getProjectCompleteDate())) {
            whereSql.append(" and project_complete_date like :projectCompleteDate");
        }
        whereSql.append(" and is_valid = 1 ");
        String userId = PlatformSessionUtils.getUserId();
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        //是否为超级管理员
        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
        //是否为省级管理员
        boolean containtRolePro = ServerUtil.isContaintRole(roles, provinceCode);
        //是否为市级管理员
        boolean containtRoleCity = ServerUtil.isContaintRole(roles, cityCode);
        //是否为县级管理员
        boolean containtRoleArea = ServerUtil.isContaintRole(roles, areaCode);
        if (!containtRole) {
            if (!containtRolePro && !containtRoleCity && !containtRoleArea) {
                whereSql.append(" and create_user =:userId");
            }else {
                if (containtRolePro) {
                    whereSql.append(" and province like :authProvince");
                }else if (containtRoleCity) {
                    whereSql.append(" and province like :authProvince");
                    whereSql.append(" and city like :authCity");
                }else {
                    whereSql.append(" and city like :authCity");
                    whereSql.append(" and area like :authArea");
                }
            }
        }
        if (!PlatformObjectUtils.isEmpty(sort)) {
            whereSql.append(" order by " + sort + " " + order);
        } else {
            whereSql.append(" order by create_time desc");
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhY02Entity.class);
        StringBuilder countSql = new StringBuilder("select count(*) from qh_y02 ");
        Query countQuery = this.em.createNativeQuery(countSql.append(whereSql).toString());
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
            countQuery.setParameter("province", queryParams.getProvince());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", queryParams.getCity());
            countQuery.setParameter("city", queryParams.getCity());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", queryParams.getArea());
            countQuery.setParameter("area", queryParams.getArea());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getVillage())) {
            query.setParameter("village", "%" + queryParams.getVillage() + "%");
            countQuery.setParameter("village", "%" + queryParams.getVillage() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillSynthesizeCode())) {
            query.setParameter("drillSynthesizeCode", "%" + queryParams.getDrillSynthesizeCode() + "%");
            countQuery.setParameter("drillSynthesizeCode", "%" + queryParams.getDrillSynthesizeCode() + "%");
        }
        //数据来源类型(SK,PK)
        if (!PlatformObjectUtils.isEmpty(queryParams.getDataSourceType())) {
            query.setParameter("dataSourceType", "%" + queryParams.getDataSourceType() + "%");
            countQuery.setParameter("dataSourceType", "%" + queryParams.getDataSourceType() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDataState())) {
            query.setParameter("dataState", queryParams.getDataState());
            countQuery.setParameter("dataState", queryParams.getDataState());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getLongitude())) {
            String longitude = queryParams.getLongitude();
            query.setParameter("longitude", "%" + longitude + "%");
            countQuery.setParameter("longitude", "%" + longitude + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getLatitude())) {
            String lat = queryParams.getLatitude();
            query.setParameter("latitude", "%" + lat + "%");
            countQuery.setParameter("latitude", "%" + lat + "%");
        }
        // 钻孔深度
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillDepthBegin())) {
            query.setParameter("drillDepthBegin", queryParams.getDrillDepthBegin());
            countQuery.setParameter("drillDepthBegin", queryParams.getDrillDepthBegin());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillDepthEnd())) {
            query.setParameter("drillDepthEnd", queryParams.getDrillDepthEnd());
            countQuery.setParameter("drillDepthEnd", queryParams.getDrillDepthEnd());
        }
        // 钻孔勘察单位
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillConstructionInstitution())) {
            String drillConstructionInstitution = queryParams.getDrillConstructionInstitution();
            query.setParameter("drillConstructionInstitution", "%" + drillConstructionInstitution + "%");
            countQuery.setParameter("drillConstructionInstitution", "%" + drillConstructionInstitution + "%");
        }
        // 勘察施工日期
        if (!PlatformObjectUtils.isEmpty(queryParams.getConstructionStartDateBegin())) {
            query.setParameter("constructionStartDateBegin", queryParams.getConstructionStartDateBegin());
            countQuery.setParameter("constructionStartDateBegin", queryParams.getConstructionStartDateBegin());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getConstructionStartDateEnd())) {
            query.setParameter("constructionStartDateEnd", queryParams.getConstructionStartDateEnd());
            countQuery.setParameter("constructionStartDateEnd", queryParams.getConstructionStartDateEnd());
        }
        // 场地类型
        if (!PlatformObjectUtils.isEmpty(queryParams.getSiteType())) {
            List<String> ids = qhY18Service.getDrillIds(queryParams.getSiteType());
            if (ids.isEmpty()) {
                ids.add("");
            }
            query.setParameter("ids", ids);
            countQuery.setParameter("ids", ids);
        }
        // 项目完成单位
        if (!PlatformObjectUtils.isEmpty(queryParams.getProjectCompleteInstitution())) {
            query.setParameter("projectCompleteInstitution", "%" + queryParams.getProjectCompleteInstitution() + "%");
            countQuery.setParameter("projectCompleteInstitution", "%" + queryParams.getProjectCompleteInstitution() + "%");
        }
        // 项目完成时间
        if (!PlatformObjectUtils.isEmpty(queryParams.getProjectCompleteDate())) {
            query.setParameter("projectCompleteDate", "%" + queryParams.getProjectCompleteDate() + "%");
            countQuery.setParameter("projectCompleteDate", "%" + queryParams.getProjectCompleteDate() + "%");
        }
        if (!containtRole) {
            if (!containtRolePro && !containtRoleCity && !containtRoleArea) {
                query.setParameter("userId", PlatformSessionUtils.getUserId());
                countQuery.setParameter("userId", PlatformSessionUtils.getUserId());
            }else {
                DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
                String divisionId = mainDept.getDivisionId();
                DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);
                String divisionName = validDivisionById.getDivisionName();
                if (containtRolePro) {
                    query.setParameter("authProvince", "%" + divisionName + "%");
                    countQuery.setParameter("authProvince", "%" + divisionName + "%");
                } else {
                    DivisionEntity division = divisionRepository.findValidDivisionById(validDivisionById.getParentId());
                    if (containtRoleCity) {
                        query.setParameter("authProvince", "%" + division.getDivisionName() + "%");
                        countQuery.setParameter("authProvince", "%" + division.getDivisionName() + "%");
                        query.setParameter("authCity", "%" + divisionName + "%");
                        countQuery.setParameter("authCity", "%" + divisionName + "%");
                    }else {
                        query.setParameter("authCity", "%" + division.getDivisionName() + "%");
                        countQuery.setParameter("authCity", "%" + division.getDivisionName() + "%");
                        query.setParameter("authArea", "%" + divisionName + "%");
                        countQuery.setParameter("authArea", "%" + divisionName + "%");
                    }
                }
            }
        }
        Pageable pageable = PageRequest.of(curPage - 1, pageSize);
        query.setFirstResult(pageable.getPageNumber() * pageable.getPageSize());
        query.setMaxResults(pageable.getPageSize());
        List<QhY02Entity> list = query.getResultList();
        BigInteger count = (BigInteger) countQuery.getSingleResult();
        Page<QhY02Entity> qhY02Page = new PageImpl<>(list, pageable, count.longValue());
        return qhY02Page;
    }

    @Override
    public List<QhY02Entity> listQhY02s(QhY02QueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from qh_y02 ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city = :city");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area = :area");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getVillage())) {
            whereSql.append(" and village like :village");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillSynthesizeCode())) {
            whereSql.append(" and drill_synthesize_code like :drillSynthesizeCode");
        }
        //数据来源类型(SK,PK)
        if (!PlatformObjectUtils.isEmpty(queryParams.getDataSourceType())) {
            whereSql.append(" and drill_synthesize_code like :dataSourceType");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDataState())) {
            whereSql.append(" and data_state like :dataState");
        }
        whereSql.append(" and is_valid = 1");
        String userId = PlatformSessionUtils.getUserId();
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        //是否为超级管理员
        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
        //是否为省级管理员
        boolean containtRolePro = ServerUtil.isContaintRole(roles, provinceCode);
        //是否为市级管理员
        boolean containtRoleCity = ServerUtil.isContaintRole(roles, cityCode);
        //是否为县级管理员
        boolean containtRoleArea = ServerUtil.isContaintRole(roles, areaCode);
        if (!containtRole) {
            if (!containtRolePro && !containtRoleCity && !containtRoleArea) {
                whereSql.append(" and create_user =:userId");
            }else {
                if (containtRolePro) {
                    whereSql.append(" and province like :authProvince");
                }else if (containtRoleCity) {
                    whereSql.append(" and province like :authProvince");
                    whereSql.append(" and city like :authCity");
                }else {
                    whereSql.append(" and city like :authCity");
                    whereSql.append(" and area like :authArea");
                }
            }
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhY02Entity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", queryParams.getCity());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", queryParams.getArea());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getVillage())) {
            query.setParameter("village", "%" + queryParams.getVillage() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillSynthesizeCode())) {
            query.setParameter("drillSynthesizeCode", "%" + queryParams.getDrillSynthesizeCode() + "%");
        }
        //数据来源类型(SK,PK)
        if (!PlatformObjectUtils.isEmpty(queryParams.getDataSourceType())) {
            query.setParameter("dataSourceType", "%" + queryParams.getDataSourceType() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDataState())) {
            query.setParameter("dataState", "%" + queryParams.getDataState() + "%");
        }
        if (!containtRole) {
            if (!containtRolePro && !containtRoleCity && !containtRoleArea) {
                query.setParameter("userId", PlatformSessionUtils.getUserId());
            }else {
                DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
                String divisionId = mainDept.getDivisionId();
                DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);
                String divisionName = validDivisionById.getDivisionName();
                if (containtRolePro) {
                    query.setParameter("authProvince", "%" + divisionName + "%");
                } else {
                    DivisionEntity division = divisionRepository.findValidDivisionById(validDivisionById.getParentId());
                    if (containtRoleCity) {
                        query.setParameter("authProvince", "%" + division.getDivisionName() + "%");
                        query.setParameter("authCity", "%" + divisionName + "%");
                    }else {
                        query.setParameter("authCity", "%" + division.getDivisionName() + "%");
                        query.setParameter("authArea", "%" + divisionName + "%");
                    }
                }
            }
        }
        List<QhY02Entity> list = query.getResultList();
        return list;
    }

    @Override
    public List<QhY02Entity> listQhY02(QhY02QueryParams queryParams) {
        StringBuilder sql = new StringBuilder("select * from qh_y02 ");
        StringBuilder whereSql = new StringBuilder(" where 1=1 ");
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            whereSql.append(" and province = :province");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            whereSql.append(" and city = :city");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            whereSql.append(" and area = :area");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getVillage())) {
            whereSql.append(" and village like :village");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillSynthesizeCode())) {
            whereSql.append(" and drill_synthesize_code like :drillSynthesizeCode");
        }
        // 场地类型
        if (!PlatformObjectUtils.isEmpty(queryParams.getSiteType())) {
            whereSql.append(" and id in :ids");
        }
        // 钻孔深度开始
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillDepthBegin())) {
            whereSql.append(" and drill_depth >= :drillDepthBegin");
        }
        // 钻孔深度结束
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillDepthEnd())) {
            whereSql.append(" and drill_depth <= :drillDepthEnd");
        }
        // 勘察施工日期开始 前端只传年月日
        if (!PlatformObjectUtils.isEmpty(queryParams.getConstructionStartDateBegin())) {
            //queryParams.setConstructionStartDateBegin(queryParams.getConstructionStartDateBegin() + "-01-01");
            whereSql.append(" and construction_start_date >= :constructionStartDateBegin");
        }
        // 勘察施工日期结束
        if (!PlatformObjectUtils.isEmpty(queryParams.getConstructionStartDateEnd())) {
            //queryParams.setConstructionStartDateEnd(queryParams.getConstructionStartDateEnd() + "-12-31");
            whereSql.append(" and construction_start_date <= :constructionStartDateEnd");
        }
        whereSql.append(" and data_state in ('2','4')");
        whereSql.append(" and is_valid = 1");
        String userId = PlatformSessionUtils.getUserId();
        List<RoleEntity> roles = PlatformAPI.getOrgAPI().getUserAPI().getRoles(userId);
        //是否为超级管理员
        boolean containtRole = ServerUtil.isContaintRole(roles, superCode);
        //是否为省级管理员
        boolean containtRolePro = ServerUtil.isContaintRole(roles, provinceCode);
        //是否为市级管理员
        boolean containtRoleCity = ServerUtil.isContaintRole(roles, cityCode);
        //是否为县级管理员
        boolean containtRoleArea = ServerUtil.isContaintRole(roles, areaCode);
        if (!containtRole) {
            if (!containtRolePro && !containtRoleCity && !containtRoleArea) {
                whereSql.append(" and create_user =:userId");
            }else {
                if (containtRolePro) {
                    whereSql.append(" and province like :authProvince");
                }else if (containtRoleCity) {
                    whereSql.append(" and province like :authProvince");
                    whereSql.append(" and city like :authCity");
                }else {
                    whereSql.append(" and city like :authCity");
                    whereSql.append(" and area like :authArea");
                }
            }
        }
        Query query = this.em.createNativeQuery(sql.append(whereSql).toString(), QhY02Entity.class);
        if (!PlatformObjectUtils.isEmpty(queryParams.getProvince())) {
            query.setParameter("province", queryParams.getProvince());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getCity())) {
            query.setParameter("city", queryParams.getCity());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getArea())) {
            query.setParameter("area", queryParams.getArea());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getVillage())) {
            query.setParameter("village", "%" + queryParams.getVillage() + "%");
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillSynthesizeCode())) {
            query.setParameter("drillSynthesizeCode", "%" + queryParams.getDrillSynthesizeCode() + "%");
        }
        // 钻孔深度
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillDepthBegin())) {
            query.setParameter("drillDepthBegin", queryParams.getDrillDepthBegin());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getDrillDepthEnd())) {
            query.setParameter("drillDepthEnd", queryParams.getDrillDepthEnd());
        }
        // 勘察施工日期
        if (!PlatformObjectUtils.isEmpty(queryParams.getConstructionStartDateBegin())) {
            query.setParameter("constructionStartDateBegin", queryParams.getConstructionStartDateBegin());
        }
        if (!PlatformObjectUtils.isEmpty(queryParams.getConstructionStartDateEnd())) {
            query.setParameter("constructionStartDateEnd", queryParams.getConstructionStartDateEnd());
        }
        // 场地类型
        if (!PlatformObjectUtils.isEmpty(queryParams.getSiteType())) {
            List<String> ids = qhY18Service.getDrillIds(queryParams.getSiteType());
            if (ids.isEmpty()) {
                ids.add("");
            }
            query.setParameter("ids", ids);
        }
        if (!containtRole) {
            if (!containtRolePro && !containtRoleCity && !containtRoleArea) {
                query.setParameter("userId", PlatformSessionUtils.getUserId());
            }else {
                DeptEntity mainDept = PlatformAPI.getOrgAPI().getUserAPI().getMainDept(userId);
                String divisionId = mainDept.getDivisionId();
                DivisionEntity validDivisionById = divisionRepository.findValidDivisionById(divisionId);
                String divisionName = validDivisionById.getDivisionName();
                if (containtRolePro) {
                    query.setParameter("authProvince", "%" + divisionName + "%");
                } else {
                    DivisionEntity division = divisionRepository.findValidDivisionById(validDivisionById.getParentId());
                    if (containtRoleCity) {
                        query.setParameter("authProvince", "%" + division.getDivisionName() + "%");
                        query.setParameter("authCity", "%" + divisionName + "%");
                    }else {
                        query.setParameter("authCity", "%" + division.getDivisionName() + "%");
                        query.setParameter("authArea", "%" + divisionName + "%");
                    }
                }
            }
        }
        List<QhY02Entity> list = query.getResultList();
        return list;
    }
}
